package com.bw.user.mvvm.viewmodel;

import android.content.Intent;
import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.user.entity.LoginEntity;
import com.bw.user.entity.RegiserEntity;
import com.bw.user.mvvm.model.RegiserModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/19 15:56
 */
public class RegiserViewModel extends BaseViewModel<RegiserModel> {

    public ObservableField<String> regusername = new ObservableField<>();
    public ObservableField<String> regpwd = new ObservableField<>();
    public ObservableField<String> reamepwd = new ObservableField<>();

    @Override
    public void oncreate() {
        super.oncreate();

    }

    @Override
    public void onDestory() {
        super.onDestory();
    }



    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity.getMessage().equals("注册成功")){
            showMsg("注册成功");
            RegiserEntity regiserEntity = (RegiserEntity) baseEntity;
            EventBus.getDefault().postSticky(regusername.get());
            UserEntity userEntity = new UserEntity();
            userEntity.setId(1l);
            userEntity.setUser_name(regusername.get());
            userEntity.setUser_pwd(regpwd.get());
            userEntity.setUser_id(3);
            userEntity.setFlag(true);
            DaoSessionUtils.getInstance().getSession().insertOrReplace(userEntity);
            finishback();
        }else {
            showMsg("注册失败");
        }
    }

    //返回
    public void finishback(){
        finishPage();
    }

    //注册按钮
    public void regiserClick(){
        if(TextUtils.isEmpty(regusername.get())||TextUtils.isEmpty(regpwd.get())){
            showMsg("用户名或密码为空");
        }else {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("mobile",regusername.get());
            hashMap.put("pwd",regpwd.get());
            hashMap.put("verifyCode","123456");
            m.requestRegiser(hashMap)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(this);
        }

    }
}
